package com.hand.demo.app.service.impl;


import com.hand.demo.domain.entity.YyfExample;
import com.hand.demo.domain.repository.YyfExampleRepository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.stream.Collectors;
import io.choerodon.mybatis.domain.AuditDomain;
import java.util.Map;

import org.springframework.stereotype.Service;
import com.hand.demo.app.service.YyfExampleService;
import java.util.List;

import org.hzero.mybatis.helper.UniqueHelper;

/**
 * 应用服务默认实现
 *
 * @author xiuhong.chen@hand-china.com 2023-09-24 21:11:43
 */
@Service
public class YyfExampleServiceImpl implements YyfExampleService {
                                                                                                
    @Autowired
    private YyfExampleRepository yyfExampleRepository;


    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<YyfExample> batchSave(List<YyfExample> yyfExampleList) {
        Map<AuditDomain.RecordStatus, List<YyfExample>> statusMap = yyfExampleList.stream().collect(Collectors.groupingBy(YyfExample::get_status));
        // 删除
        if (statusMap.containsKey(AuditDomain.RecordStatus.delete)) {
            List<YyfExample> deleteList = statusMap.get(AuditDomain.RecordStatus.delete);
            yyfExampleRepository.batchDeleteByPrimaryKey(deleteList);
        }
        // 更新
        if (statusMap.containsKey(AuditDomain.RecordStatus.update)) {
            List<YyfExample> updateList = statusMap.get(AuditDomain.RecordStatus.update);
            updateList.forEach(item -> {
                // TODO: 唯一性校验
                UniqueHelper.valid(item,YyfExample.YYF_EXAMPLE_U1);
                yyfExampleRepository.updateByPrimaryKeySelective(item);
            });
        }
        // 新增
        if (statusMap.containsKey(AuditDomain.RecordStatus.create)) {
            List<YyfExample> createList = statusMap.get(AuditDomain.RecordStatus.create);
            createList.forEach(item -> {
                // TODO: 唯一性校验
                UniqueHelper.valid(item,YyfExample.YYF_EXAMPLE_U1);
                yyfExampleRepository.insertSelective(item);
            });
        }
        return yyfExampleList;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public YyfExample save(YyfExample yyfExample) {
        //保存
        UniqueHelper.valid(yyfExample,YyfExample.YYF_EXAMPLE_U1);
        if (yyfExample.getNoticeId() == null) {
            yyfExampleRepository.insertSelective(yyfExample);
        } else {
            yyfExampleRepository.updateOptional(yyfExample,
                    YyfExample.FIELD_ORIGIN_HOST,
                    YyfExample.FIELD_NOTICE_TOPIC,
                    YyfExample.FIELD_ORIGIN_PORT,
                    YyfExample.FIELD_ORIGIN_SERVICE_NAME,
                    YyfExample.FIELD_NOTICE_CONTENT,
                    YyfExample.FIELD_SEND_DATE_TIME,
                    YyfExample.FIELD_RECEIVER_COUNT,
                    YyfExample.FIELD_SEND_STATUS,
                    YyfExample.FIELD_RETRYABLE_FLAG
            );
        }

        return yyfExample;
    }
}
